<!--  -->
<template>
  <div
    class="AndreInput"
    :class="{ 'AndreInput--suffix': clearable || showPassword }"
  >
    <div class="autoBox">
      <input
        class="AndreInput__inner"
        :placeholder="placeholder"
        :type="showPassword ? (passwordVisible ? 'text' : 'password') : type"
        :name="name"
        :disabled="disabled"
        :class="[{ 'is-disabled': disabled }]"
        @input="handleInput"
        :value="value"
      />
      <span class="AndreInput__suffix" v-if="clearable || showPassword">
        <i
          class="Andre-icon-guanbi"
          v-if="clearable && value && type != 'password'"
          @click="clear"
        ></i>
        <i
          :style="{ 'color': blue }"
          class="Andre-icon-yanjing"
          v-if="showPassword"
          @click="onTestAndPassword"
        ></i>
      </span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AndreInput',
  props: {
    placeholder: {
      type: String,
      default: '请输入'
    },
    type: {
      type: String,
      default: 'text'
    },
    name: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    value: {
      type: [String, Number],
      default: ''
    },
    clearable: {
      type: Boolean,
      default: false
    },
    showPassword: {
      type: Boolean,
      default: false
    }
  },
  // import引入的组件需要注入到对象中才能使用
  components: {},
  data () {
    // 这里存放数据
    return {
      passwordVisible: 'text',
      blue: ''
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    handleInput (e) {
      this.$emit('input', e.target.value)
    },
    clear () {
      this.$emit('input', '')
    },
    onTestAndPassword () {
      this.passwordVisible = !this.passwordVisible
      if (this.passwordVisible === false) {
        this.blue = 'blue'
      } else {
        this.blue = ''
      }
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {},
  beforeCreate () {}, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeDestroy () {}, // 生命周期 - 销毁之前
  destroyed () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang="scss" scoped>
//@import url(); 引入公共css类
.AndreInput {
  width: 100%;
  position: relative;
  font-size: 14px;
  display: inline-block;
  .AndreInput__inner {
    -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 40px;
    line-height: 40px;
    outline: none;
    padding: 0 15px;
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    width: 100%;

    &:focus {
      outline: none;
      border-color: #409eff;
    }
    &.is-disabled {
      background-color: #f5f7fa;
      border-color: #e4e7ed;
      color: #c0c4cc;
      cursor: not-allowed;
    }
  }
}

.AndreInput--suffix {
  .AndreInput__inner {
    padding-right: 30px;
  }
  .AndreInput__suffix {
    position: absolute;
    height: 100%;
    right: 10px;
    top: 0;
    line-height: 40px;
    text-align: center;
    color: #c0c4cc;
    transition: all 0.3s;
    z-index: 900;
    i {
      color: #c0c4cc;
      font-size: 14px;
      cursor: pointer;
      transition: color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    }
  }
}
.blue {
  color: blue;
}
</style>
